<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工厂" prop="factoryId">
        <SelectFactory v-model="queryParams.factoryId"></SelectFactory>
      </el-form-item>
      <el-form-item label="单号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="生产计划" prop="planId">
        <el-input v-model="queryParams.planId" placeholder="请输入生产计划" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="批次号" prop="batchNo">
        <el-input v-model="queryParams.batchNo" placeholder="请输入批次号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="ERP单号" prop="erpDocNo">
        <el-input v-model="queryParams.erpDocNo" placeholder="请输入ERP单据编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="单据类型" prop="erpDocCode">
        <el-input v-model="queryParams.erpDocCode" placeholder="请输入单据类型" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="工作中心" prop="erpCenterCode">
        <el-input v-model="queryParams.erpCenterCode" placeholder="请输入工作中心" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_INTEGER_STATUS)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="单据日期" prop="erpDocDate">
        <el-date-picker v-model="queryParams.erpDocDate" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
<!--    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['wms:erp-produce-in:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['wms:erp-produce-in:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" prop="id" fixed />
      <el-table-column label="单号" align="center" prop="code" fixed width="180px">
<!--        <template v-slot="scope">
          <el-button type="text" @click="handleInfo(scope.row)"
                     v-hasPermi="['wms:erp-produce-in:query']">{{scope.row.code}}</el-button>
        </template>-->
      </el-table-column>
      <el-table-column label="工厂" align="center" prop="factoryName" width="100px" />
      <el-table-column label="生产车间" align="center" prop="workshopName" width="100px" />
      <el-table-column label="ERP单号" align="center" prop="erpDocNo" width="180px" />
      <el-table-column label="ERP单据类型" align="center" prop="erpDocCode" width="150px" />
      <el-table-column label="ERP单据日期" align="center" prop="erpDocDate" width="150px" />
      <el-table-column label="ERP工作中心" align="center" prop="erpCenterCode" width="150px" />
      <el-table-column label="ERP仓库" align="center" prop="erpWarehouseCode" width="100px" />
      <el-table-column label="ERP库位" align="center" prop="erpBinCode" width="100px" />
      <el-table-column label="生产计划" align="center" prop="planCode" width="180px" show-overflow-tooltip  />
      <el-table-column label="产品名称" align="center" prop="itemName" width="180px" show-overflow-tooltip />
      <el-table-column label="产品代码" align="center" prop="itemCode" width="180px" show-overflow-tooltip />
      <el-table-column label="产品规格" align="center" prop="itemSpec" width="180px" show-overflow-tooltip />
      <el-table-column label="产品图号" align="center" prop="drawCode" width="180px" show-overflow-tooltip />
      <el-table-column label="允收数量" align="center" prop="acceptedQty" />
      <el-table-column label="报废数量" align="center" prop="scrapQty" />
      <el-table-column label="破坏数量" align="center" prop="destroyedQty" />
      <el-table-column label="批次号" align="center" prop="batchNo" width="180px" />
      <el-table-column label="ERP状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_APPROVE" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="请求信息" align="center" prop="request" show-overflow-tooltip />
      <el-table-column label="返回信息" align="center" prop="response" show-overflow-tooltip />
      <el-table-column label="响应代码" align="center" prop="respCoode" width="100px" show-overflow-tooltip />
      <el-table-column label="响应信息" align="center" prop="description" show-overflow-tooltip />
      <el-table-column label="备注" align="center" prop="remark" width="180px" show-overflow-tooltip="" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="100px">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-close" @click="handleCancel(scope.row)"
                     v-hasPermi="['wms:erp-produce-in:cancel']">撤销</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工厂" prop="code">
          <el-input v-model="form.factoryId" placeholder="请输入单号" />
        </el-form-item>
        <el-form-item label="单号" prop="code">
          <el-input v-model="form.code" placeholder="请输入单号" />
        </el-form-item>
        <el-form-item label="单据编号" prop="erpDocNo">
          <el-input v-model="form.erpDocNo" placeholder="请输入单据编号" />
        </el-form-item>
        <el-form-item label="单据类型" prop="erpDocCode">
          <el-input v-model="form.erpDocCode" placeholder="请输入单据类型" />
        </el-form-item>
        <el-form-item label="单据日期" prop="erpDocDate">
          <el-date-picker clearable v-model="form.erpDocDate" type="date" value-format="timestamp" placeholder="选择单据日期" />
        </el-form-item>
        <el-form-item label="工作中心" prop="erpCenterCode">
          <el-input v-model="form.erpCenterCode" placeholder="请输入工作中心" />
        </el-form-item>
        <el-form-item label="仓库" prop="erpWarehouseCode">
          <el-input v-model="form.erpWarehouseCode" placeholder="请输入仓库" />
        </el-form-item>
        <el-form-item label="库位" prop="erpBinCode">
          <el-input v-model="form.erpBinCode" placeholder="请输入库位" />
        </el-form-item>
        <el-form-item label="生产计划" prop="planId">
          <el-input v-model="form.planId" placeholder="请输入生产计划" />
        </el-form-item>
        <el-form-item label="允收数量" prop="acceptedQty">
          <el-input v-model="form.acceptedQty" placeholder="请输入允收数量" />
        </el-form-item>
        <el-form-item label="报废数量" prop="scrapQty">
          <el-input v-model="form.scrapQty" placeholder="请输入报废数量" />
        </el-form-item>
        <el-form-item label="破坏数量" prop="destroyedQty">
          <el-input v-model="form.destroyedQty" placeholder="请输入破坏数量" />
        </el-form-item>
        <el-form-item label="批次号" prop="batchNo">
          <el-input v-model="form.batchNo" placeholder="请输入批次号" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_INTEGER_STATUS)"
                      :key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="请求信息" prop="request">
          <el-input v-model="form.request" placeholder="请输入请求信息" />
        </el-form-item>
        <el-form-item label="返回信息" prop="response">
          <el-input v-model="form.response" placeholder="请输入返回信息" />
        </el-form-item>
        <el-form-item label="同步信息">
          <editor v-model="form.description" :min-height="192"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createErpProduceIn, updateErpProduceIn, deleteErpProduceIn, getErpProduceIn, getErpProduceInPage, exportErpProduceInExcel, cancelErpProduceIn } from "@/api/wms/erpProduceIn";
import Editor from '@/components/Editor';
import SelectFactory from '@/components/SelectFactory/index.vue'

export default {
  name: "ErpProduceIn",
  components: {
    SelectFactory,
    Editor
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // erp生产入库列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        factoryId: null,
        code: null,
        erpDocNo: null,
        erpDocCode: null,
        erpDocDate: [],
        erpCenterCode: null,
        planId: null,
        batchNo: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [{ required: true, message: "单号不能为空", trigger: "blur" }],
        erpDocNo: [{ required: true, message: "单据编号不能为空", trigger: "blur" }],
        erpDocCode: [{ required: true, message: "单据类型不能为空", trigger: "blur" }],
        erpDocDate: [{ required: true, message: "单据日期不能为空", trigger: "blur" }],
        erpCenterCode: [{ required: true, message: "工作中心不能为空", trigger: "blur" }],
        erpWarehouseCode: [{ required: true, message: "仓库不能为空", trigger: "blur" }],
        planId: [{ required: true, message: "生产计划不能为空", trigger: "blur" }],
        acceptedQty: [{ required: true, message: "允收数量不能为空", trigger: "blur" }],
        scrapQty: [{ required: true, message: "报废数量不能为空", trigger: "blur" }],
        destroyedQty: [{ required: true, message: "破坏数量不能为空", trigger: "blur" }],
        batchNo: [{ required: true, message: "批次号不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getErpProduceInPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        factoryId: undefined,
        code: undefined,
        erpDocNo: undefined,
        erpDocCode: undefined,
        erpDocDate: undefined,
        erpCenterCode: undefined,
        erpWarehouseCode: undefined,
        erpBinCode: undefined,
        planId: undefined,
        acceptedQty: undefined,
        scrapQty: undefined,
        destroyedQty: undefined,
        batchNo: undefined,
        status: undefined,
        request: undefined,
        response: undefined,
        description: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加erp生产入库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateErpProduceIn(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createErpProduceIn(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 查询详情 */
    handleInfo(row) {
      const id = row.id;
      getErpProduceIn(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "ERP生产入库详情";
      });
    },
    /** 撤销按钮操作 */
    handleCancel(row) {
      const id = row.id;
      this.$modal.confirm('是否确认撤销erp生产入库编号为"' + id + '"的数据项?').then(function() {
        return cancelErpProduceIn(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("撤销成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有erp生产入库数据项?').then(() => {
          this.exportLoading = true;
          return exportErpProduceInExcel(params);
        }).then(response => {
          this.$download.excel(response, 'erp生产入库.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
